package top.so.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import top.so.dto.BlogArticleDTO;
import top.so.dto.ColumnDTO;
import top.so.dto.RankPublishDTO;
import top.so.entity.RankPublish;
import top.so.service.BlogArticleService;
import top.so.service.ColumnService;

import java.util.List;

@Controller
@RequestMapping("/rank")
public class RankController {

    @Autowired
    private BlogArticleService blogArticleService;
    @Autowired
    private ColumnService columnService;

    @RequestMapping(value = "")
    public ModelAndView rank(){
        ModelAndView mv = new ModelAndView("main/Rank");
        //文章排行榜
        List<BlogArticleDTO> blogArticleDTOList = blogArticleService.selectOrderByReadNum(new BlogArticleDTO());
        if(blogArticleDTOList!=null&&!blogArticleDTOList.isEmpty()){
            for(BlogArticleDTO blogArticleDTO:blogArticleDTOList){
                mv.addObject("blogArticleDTO",blogArticleDTO);
                blogArticleDTOList.remove(blogArticleDTO);
                break;
            }
        }
        if(!blogArticleDTOList.isEmpty())
        mv.addObject("blogArticleDTOList",blogArticleDTOList);

        //专栏排行
        List<ColumnDTO> columnDTOList = columnService.selectOrderByReadNum(new ColumnDTO());
        if(columnDTOList!=null&&!columnDTOList.isEmpty()){
            for(ColumnDTO columnDTO:columnDTOList){
                mv.addObject("columnDTO",columnDTO);
                columnDTOList.remove(columnDTO);
                break;
            }
        }
        if(!columnDTOList.isEmpty()){
            mv.addObject("columnDTOList",columnDTOList);
        }

        //博客排行
        List<RankPublishDTO> rankPublishDTOList = blogArticleService.selectRankPublish();
        if(rankPublishDTOList !=null&&!rankPublishDTOList.isEmpty()){
            for(RankPublishDTO rankPublishDTO : rankPublishDTOList){
                mv.addObject("rankPublishDTO", rankPublishDTO);
                rankPublishDTOList.remove(rankPublishDTO);
                break;
            }
        }
        if(!rankPublishDTOList.isEmpty()){
            mv.addObject("rankPublishDTOList", rankPublishDTOList);
        }


        return mv;
    }
}
